package org.boulderfoodrescue.webapp.client.donors;

import java.util.Comparator;

import org.boulderfoodrescue.webapp.client.resources.Resources;
import org.boulderfoodrescue.webapp.shared.Location;

import com.google.gwt.cell.client.ClickableTextCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.view.client.ListDataProvider;

public class DonorTable extends FlowPanel{
	protected CellTable<Location> table;
	protected ListDataProvider<Location> dataProvider;
	public DonorTable(final DonorRecipientInterface donorInterface){
		dataProvider = new ListDataProvider<Location>();
		table = new CellTable<Location>();

		/* Create all the columns */

		ClickableTextCell nameCell = new ClickableTextCell();
		Column<Location, String> nameColumn = new Column<Location, String>(nameCell){
			@Override
			public String getValue(Location object) {
				return object.getName();
			}
		};

		TextColumn<Location> foodTypeColumn = new TextColumn<Location>(){
			@Override
			public String getValue(Location object) {
				return object.getFoodType().toString();
			}
		};

		TextColumn<Location> notesColumn = new TextColumn<Location>(){
			@Override
			public String getValue(Location object) {
				return object.getNotes();
			}
		};

		ClickableTextCell pickupTimesCell = new ClickableTextCell();
		Column<Location, String> pickupTimes = new Column<Location, String>(pickupTimesCell){
			@Override
			public String getValue(Location object) {
				return "Pickup Times";
			}
		};

		TextColumn<Location> ownerColumn = new TextColumn<Location>(){
			@Override
			public String getValue(Location object) {
				return object.getCreatorEmail();
			}
		};
		pickupTimes.setCellStyleNames(Resources.INSTANCE.ops().textButtonCell());
		
		ClickableTextCell editCell = new ClickableTextCell();
		Column<Location, String> editColumn = new Column<Location, String>(editCell){
			@Override
			public String getValue(Location object) {
				return "Edit";
			}
		};
		editColumn.setCellStyleNames(Resources.INSTANCE.ops().textButtonCell());

		/* Add the columns to the table */
		table.addColumn(nameColumn, "Name");
		table.addColumn(foodTypeColumn, "Food Type");
		table.addColumn(notesColumn, "Notes");
		table.addColumn(ownerColumn, "Owner");
		table.addColumn(pickupTimes, "Pickup Times");
		table.addColumn(editColumn, "Edit");

		dataProvider.addDataDisplay(table);

		ListHandler<Location> sortHandler = new ListHandler<Location>(dataProvider.getList());
		table.addColumnSortHandler(sortHandler);

		/* Set the columns as sortable and add their comparators to the sort handler */
		nameColumn.setSortable(true);
		sortHandler.setComparator(nameColumn, new Comparator<Location>(){
			@Override
			public int compare(Location o1, Location o2) {
				return o1.getName().compareTo(o2.getName());
			}
		});

		foodTypeColumn.setSortable(true);
		sortHandler.setComparator(foodTypeColumn, new Comparator<Location>(){
			@Override
			public int compare(Location o1, Location o2) {
				return o1.getFoodType().toString().compareTo(o2.getFoodType().toString());
			}
		});

		ownerColumn.setSortable(true);
		sortHandler.setComparator(ownerColumn, new Comparator<Location>(){
			@Override
			public int compare(Location o1, Location o2) {
				return o1.getCreatorEmail().compareTo(o2.getCreatorEmail());
			}
		});

		pickupTimes.setFieldUpdater(new FieldUpdater<Location, String>(){
			@Override
			public void update(int index, final Location object, String value) {
				donorInterface.onTimeSlotViewOpen(object);
			}
		});
		
		editColumn.setFieldUpdater(new FieldUpdater<Location, String>(){
			@Override
			public void update(int index, final Location object, String value) {
				donorInterface.onEditLocation(object);
			}
		});


		this.add(table);
		table.setWidth("100%");
		//this.setHeight("100%");
		table.setEmptyTableWidget(new HTML("No pickups are available to view."));
	}
	public void addLocation(Location location){
		dataProvider.getList().add(location);
	}
	public void removeLocation(Location location){
		dataProvider.getList().remove(location);
	}
	public void updateLocation()
	{
		table.redraw();
	}
}
